package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.album.*;
import com.atguigu.tingshu.vo.category.CategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;
	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


    @Override
    public List<CategoryVo> getBaseCategoryList() {
        return baseCategoryViewMapper.getBaseCategoryList();
    }

	@Override
	public List<BaseAttribute> findAttribute(Long cId) {
		return baseAttributeMapper.findAttribute(cId);
	}

    @Override
    public BaseCategoryView getAlbumInfoCategory(Long albumId) {
        return baseCategory3Mapper.getAlbumInfoCategory(albumId);
    }

    @Override
    public List<BaseCategory3> getC1Id7TopBaseCategory3List(Long c1Id) {
		//去二级分类对象中根据一级分类查询二级分类对象信息
		LambdaQueryWrapper<BaseCategory2> queryWrapper = new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, c1Id)
				.eq(BaseCategory2::getIsDeleted, 0);
		List<BaseCategory2> baseCategory2s = baseCategory2Mapper.selectList(queryWrapper);
		if (null==baseCategory2s){
			throw new GuiguException(201,"该一级分类下没有任何二级分类");
		}

		List<Long> c2Id = baseCategory2s.stream().map(BaseCategory2::getId).collect(Collectors.toList());
		LambdaQueryWrapper<BaseCategory3> queryWrapper1 = new LambdaQueryWrapper<>();
		queryWrapper1.in(BaseCategory3::getCategory2Id,c2Id)
				.eq(BaseCategory3::getIsTop,1)
				.last("limit 7")
				.eq(BaseCategory3::getIsDeleted,0);
		List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(queryWrapper1);
		 if (CollectionUtils.isEmpty(baseCategory3List)){
			 return new ArrayList<>();
		 }
		return baseCategory3List;
    }

	@Override
	public CategoryVo getC1IdBaseCategoryList(Long c1Id) {

		//1.根据一级分类id查询一级分类对象
		BaseCategory1 baseCategory1 = baseCategory1Mapper.selectById(c1Id);
		if (baseCategory1==null){
			throw new GuiguException(201,"一级分类不存在");
		}

		//2.根据一级分类id查询所有二级分类
		List<BaseCategory2> baseCategory2s  = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, c1Id));
		//2.1二级分类是一个集合装进去
		ArrayList<CategoryVo> categoryVos = new ArrayList<>();
		if (CollectionUtils.isEmpty(baseCategory2s)){
			throw new GuiguException(201,"一级分类下没有二级分类集合");
		}
		//二级分类集合内容
		for (BaseCategory2 baseCategory2 : baseCategory2s) {
			CategoryVo categoryVo2 = new CategoryVo();
			categoryVo2.setCategoryId(baseCategory2.getId());
			categoryVo2.setCategoryName(baseCategory2.getName());
			ArrayList<CategoryVo> categoryVos2 = new ArrayList<>();

			//查询二级分类的孩子
			List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>().eq(BaseCategory3::getCategory2Id, baseCategory2.getId()));
			for (BaseCategory3 baseCategory3 : baseCategory3List) {
				CategoryVo categoryVo3 = new CategoryVo();
				categoryVo3.setCategoryId(baseCategory3.getId());
				categoryVo3.setCategoryName(baseCategory3.getName());
				categoryVo3.setCategoryChild(Lists.newArrayList());//三级分类的孩子
				categoryVos2.add(categoryVo3);
			}
			categoryVo2.setCategoryChild(categoryVos2);//二级分类的孩子
			categoryVos.add(categoryVo2);
		}

		//一级分类对象
		CategoryVo categoryVo = new CategoryVo();
		categoryVo.setCategoryId(c1Id);//一级分类id
		categoryVo.setCategoryName(baseCategory1.getName());
		categoryVo.setCategoryChild(categoryVos);//孩子

		return categoryVo;
	}

    @Override
    public List<Long> getCategory1IdList() {
		List<BaseCategory1> baseCategory1s = baseCategory1Mapper.selectList(null);
		return baseCategory1s.stream().map(BaseCategory1::getId).collect(Collectors.toList());
    }
}
